package cn.jdy.plugins.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Company;
import cn.jdy.plugins.entity.CompanyContext;
import cn.jdy.plugins.entity.GuseBook;
import cn.jdy.plugins.service.CompanyContextService;
import cn.jdy.plugins.service.CompanyService;
import cn.jdy.plugins.service.GuseBookService;

/**
 * 加盟留言控制类
 *
 * @author GXP
 */
@Controller
@RequestMapping("/gusebook")
public class GuseBookCtrl extends BaseCtrl {

	@Autowired
	private GuseBookService guseBookService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private CompanyContextService companyContextService;

	/**
	 * 初始化公共在线留言页面
	 * @param gusebook
	 * @return
	 */
	@RequestMapping("/initPub")
	public ModelAndView initPub(GuseBook gusebook) {
		ModelAndView mv = new ModelAndView();
		Company company = companyService.findOne("id", Integer.valueOf(gusebook.getCompanyId()));
		mv.addObject("company", company);
		mv.setViewName("/company/lealwords.jsp");
		return mv;
	}
	
	/**
	 * ajax 提交保存留言信息
	 * @return
	 */
	@RequestMapping("/saveLealwordajax")
	@ResponseBody
	public Map<String, Object> saveLealwordajax(GuseBook gusebook) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			gusebook.setId(null);
			gusebook.setIp(request.getRemoteAddr());
			gusebook.setGusetime(new Date());
			guseBookService.save(gusebook);
			jsonMap.put("status", 200);
		} catch (Exception e) {
			jsonMap.put("status", 500);
			BaseLog.e(this.getClass(), "保存留言信息失败", e);
		}
		
		return jsonMap;
	}
	
	/**
	 * 添加加盟留言
	 *
	 * @param gusebook
	 * @return
	 */
	@RequestMapping("/add")
	public String addGuseBook(GuseBook gusebook) {
		ModelAndView mv = new ModelAndView();
		Company company = companyService.findOne("id", Integer.valueOf(gusebook.getCompanyId()));
		gusebook.setId(null);
		gusebook.setIp(request.getRemoteAddr());
		gusebook.setGusetime(new Date());
		guseBookService.save(gusebook);
		mv.addObject("company", company);
		mv.setViewName("/company/gusebook.jsp");
		return "redirect:/gusebook/getPageList/" + gusebook.getCompanyId();
	}

	 /**
	   * 加盟留言
	   * @return
	   */
	@RequestMapping("/getPageList/{companyId}")
	public ModelAndView getPageList(@PathVariable("companyId") Integer companyId,String view,Integer pageNo) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/company/gusebook.jsp");
		try {
			int pageSize = 10;
			Company company = companyService.findOne("id", companyId);
			CompanyContext context = companyContextService.findOne("companyId", companyId);
			mv.addObject("company", company);
			mv.addObject("context", context);

			/************* 分页处理开始 ************/
			int guseTotal = guseBookService.getCount(companyId);
			Map<String, Integer> guseMap = getPageInfo(pageNo, pageSize, String.valueOf(guseTotal));// 获取page信息
			pageNo = guseMap.get("currentPage");
			pageSize = guseMap.get("pageSize");
			Integer totalPage = guseMap.get("totalPage");
			/************* 分页处理结束 ***********/

			List<GuseBook> guseList = guseBookService.getList(companyId, pageNo, pageSize);
			mv.addObject("list", guseList);
			mv.addObject("pageNo", pageNo);
			mv.addObject("total", guseTotal);
			mv.addObject("size", pageSize);
			
			mv.addObject("companyId", companyId);
			mv.addObject("view",view);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "加盟留言记录查询失败", e);
		}
		return mv;
	}
}
